# FindArduino.cmake
#
# find_package( Arduino .... todo: fix this <---
#
#
#

# This file is built on the sholders of these giants:
#   http://mjo.tc/atelier/2009/02/arduino-cli.html
#   http://johanneshoff.com/arduino-command-line.html
#   http://www.arduino.cc/playground/Code/CmakeBuild
#   http://www.tmpsantos.com.br/en/2010/12/arduino-uno-ubuntu-cmake/
#   http://  -- fix: QueezyTheGreat?



option(ARDUINO_USE_MAIN "Clear this value to use your own main() function" 1)
if(NOT ARDUINO_USE_MAIN)
  message(WARNING "You will need to provide your own main().")
endif()



set(ARDUINO_SEARCH_PATHS
  ~
  ~/installs/
  ~/installs/arduino
)

if( LINUX )
  set(ARDUINO_SEARCH_PATHS ${ARDUINO_SEARCH_PATHS}
    /usr/local
    /usr/local/arduino
    /usr/local/share
    /usr/local/share/arduino
    /usr/share
    /opt
  )

elseif( APPLE )

  set(ARDUINO_SEARCH_PATHS ${ARDUINO_SEARCH_PATHS}
    /Applications/Arduino.app/Contents/Resources/Java
  )

elseif( WIN32 )

  set(ARDUINO_SEARCH_PATHS ${ARDUINO_SEARCH_PATHS}
    C:/arduino-0022/arduino-0022
  )

endif()


# From Queezy:
set(ARDUINO_VERSION_NAMES arduino)
#  ***todo***  version.  If version is given in the arg list, we should add it next and skip the others
# if( version)
#    list(APPEND ARDUINO_VERSION_NAMES arduino-00${command line VERSION})
#else()  #endif()
foreach(VERSION_MAJOR RANGE 1 5)
  foreach(VERSION_MINOR RANGE 0 10)
    list(APPEND ARDUINO_VERSION_NAMES arduino-${VERSION_MAJOR}.${VERSION_MINOR})
    foreach(VERSION_BUILD RANGE 0 99)
      list(APPEND ARDUINO_VERSION_NAMES arduino-${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_BUILD})
    endforeach()
  endforeach()
endforeach()
foreach(VERSION RANGE 99 20)
    list(APPEND ARDUINO_VERSION_NAMES arduino-00${VERSION})
endforeach()

#
# We need to fix the search
# old has only this:
#     hardware/arduino/sam/boards.txt
# new has both:
#     hardware/arduino/avr/boards.txt
#     hardware/arduino/sam/boards.txt
#
#

#
# work around, just for now
#
set(CHIP
  avr   # could have been sam
)


FIND_PATH(ARDUINO_SDK_PATH
    hardware/arduino/${CHIP}/boards.txt   # 1.5?
    hardware/arduino/boards.txt
  PATHS
    ${ARDUINO_SEARCH_PATHS}
  PATH_SUFFIXES
    ${ARDUINO_VERSION_NAMES}
  DOC
    "Arduino SDK Path"
)

FIND_PATH(ARDUINO_INCLUDE_DIR
    WProgram.h
    Arduino.h
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    hardware/arduino/${CHIP}/cores/arduino
    hardware/arduino/cores/arduino
    arduino
)

FIND_FILE(ARDUINO_HEADER_FILE
  NAMES
    WProgram.h
    Arduino.h
  PATHS
    ${ARDUINO_INCLUDE_DIR}
  DOC "This is the common Arduino header file."
)

FIND_PATH(ARDUINO_EXTRA_LIB_DIR
    Wire
    LiquidCrystal
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    libraries
)


FIND_FILE(ARDUINO_BOARDS_FILE boards.txt
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    hardware/arduino/${CHIP}
    hardware/arduino
  DOC "This is the Arduino boards.txt file.  It contains descriptions of the various boards."
)

FIND_FILE(ARDUINO_PROGRAMMERS_FILE  programmers.txt
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    hardware/arduino/${CHIP}
    hardware/arduino
  DOC "This is the Arduino programmers.txt file.  It contains descriptions of the various programmers."
)


FIND_FILE(ARDUINO_VERSION_FILE  version.txt
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    lib
  DOC "This file contains the version number."
)


FIND_PATH(ARDUINO_VARIANTS_ROOT_DIR standard/pins_arduino.h  # variants is optional.  Exists in 1.0 but not 0022
  PATHS
    ${ARDUINO_SDK_PATH}
  PATH_SUFFIXES
    hardware/arduino/${CHIP}/variants
    hardware/arduino/variants
)


# handle the QUIETLY and REQUIRED arguments and set ARDUINO_FOUND to
# TRUE if all listed variables are TRUE
#INCLUDE(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(ARDUINO DEFAULT_MSG
  ARDUINO_INCLUDE_DIR
  ARDUINO_EXTRA_LIB_DIR
  ARDUINO_BOARDS_FILE
  ARDUINO_VERSION_FILE
)

if( ARDUINO_FOUND )
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/version.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/boards.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/libraries.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/flash.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/arduinoPlatform.cmake)
  include(${CMAKE_CURRENT_LIST_DIR}/arduino/wrap_sketch.cmake)

  # which board to build for?
  if(NOT ARDUINO_PLATFORM)
    set(ARDUINO_PLATFORM $ENV{ARDUINO_PLATFORM})
    # if we didn't find it error out.
    if(NOT ARDUINO_PLATFORM)
      message(SEND_ERROR "Missing ARDUINO_PLATFORM.  Set in CMake or environment and retry.")
      show_arduino_platforms()
    endif()
  endif()


  if(ARDUINO_PLATFORM)
    # upcase an inbound platform and then make the call to compile the libraries and set linker flags, etc.
    string(TOUPPER "${ARDUINO_PLATFORM}" ARDUINO_PLATFORM)
    set_arduino_platform(${ARDUINO_PLATFORM})
  endif()

  MARK_AS_ADVANCED(ARDUINO_INCLUDE_DIR)
endif()

